package com.pkutech.carcasher.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.pkutech.carcasher.R;
import com.pkutech.carcasher.content.HmSelectFormalOrderDataSource;
import com.pkutech.carcasher.content.PaySelectFormalOrderAdapter;
import com.pkutech.carcasher.model.HmSelectFormalOrder;
import com.pkutech.carcasher.utils.CommonUtils;
import com.shizhefei.mvc.MVCHelper;
import com.shizhefei.mvc.MVCSwipeRefreshHelper;

import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * "缴费服务"模块
 * Created by cyy on 2016/6/10.
 */
public class PaymentServiceActivity extends BaseActivity {

    public static final int PAY_FEES_REQUEST_CODE = 121;

    public static final String SELECT_FORMAL_ORDER = "SELECT_FORMAL_ORDER";
    @BindView(R.id.toolbar)
    Toolbar toolbar;

    TimePickerView endTime;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.listView)
    ListView listView;
    @BindView(R.id.timeTxt)
    TextView timeTxt;
    @BindView(R.id.timeLayout)
    LinearLayout timeLayout;

    private MVCHelper<List<HmSelectFormalOrder>> mvcHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_payment_service);
        ButterKnife.bind(this);
        initToolbar(toolbar);

        //时间选择器
        endTime = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY);
        //控制时间范围
//        Calendar calendar = Calendar.getInstance();
//        pvTime.setRange(calendar.get(Calendar.YEAR) - 20, calendar.get(Calendar.YEAR));
        endTime.setTime(new Date());
        endTime.setCyclic(false);
        endTime.setCancelable(true);
        //时间选择后回调
        endTime.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {

            @Override
            public void onTimeSelect(Date date) {
                timeTxt.setText(CommonUtils.getDate(date));
                refreshListWithDate(timeTxt.getText().toString());
            }
        });
        //弹出时间选择器
        timeLayout.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                endTime.show();
            }
        });

        mvcHelper = new MVCSwipeRefreshHelper<List<HmSelectFormalOrder>>(swipeRefreshLayout);
        // 设置数据源
        mvcHelper.setDataSource(new HmSelectFormalOrderDataSource(1));
        // 设置适配器
        mvcHelper.setAdapter(new PaySelectFormalOrderAdapter(this));
        String curDate = CommonUtils.getDate(new Date());
        HmSelectFormalOrderDataSource hmSelectFormalOrderDataSource = (HmSelectFormalOrderDataSource)mvcHelper.getDataSource();
        hmSelectFormalOrderDataSource.date =  curDate;
        timeTxt.setText(curDate);
        // 加载数据
        mvcHelper.refresh();

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                HmSelectFormalOrder selectFormalOrder = (HmSelectFormalOrder) view.getTag(R.string.formal_order_item_id);
                Intent intent = new Intent();
                intent.putExtra(SELECT_FORMAL_ORDER, selectFormalOrder);
                intent.setClass(PaymentServiceActivity.this, PayFeesActivity.class);
                startActivityForResult(intent, PAY_FEES_REQUEST_CODE);
            }
        });
    }

    private void refreshListWithDate(String date){
        if(!TextUtils.isEmpty(date)){
            HmSelectFormalOrderDataSource hmSelectFormalOrderDataSource = (HmSelectFormalOrderDataSource)mvcHelper.getDataSource();
            hmSelectFormalOrderDataSource.date = date;
        }
        // 加载数据
        mvcHelper.refresh();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case PAY_FEES_REQUEST_CODE:
                if(mvcHelper != null){
                    mvcHelper.refresh();
                }
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 释放资源
        mvcHelper.destory();
    }

}
